package com.gec.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gec.common.R;
import com.gec.dto.OrderQuery;
import com.gec.dto.OrdersDto;
import com.gec.entity.Orders;
import com.gec.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.apiguardian.api.API;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

/**
 * <p>
 *  订单管理
 * </p>
 *
 * @author : [chenxys]
 * @version : [v1.0]
 * @createTime : [2022/7/14 18:19]
 */

@Slf4j
@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private OrderService orderService;

    /**
     * 分页查询
     * @author chenxys
     * @param page 当前页
     * @param pageSize 每页显示条数
     * @param number 订单号
     * @param beginTime 开始时间
     * @param endTime 结束时间
     * @return com.gec.common.R<com.baomidou.mybatisplus.extension.plugins.pagination.Page>
     */
    @GetMapping("/page")
    public R<Page> page(@RequestParam(required = true) Long page,
                        @RequestParam(required = true) Long pageSize,
                        @RequestParam(required = false) String number,
                        @RequestParam(required = false) LocalDateTime beginTime,
                        @RequestParam(required = false) LocalDateTime endTime){
        OrderQuery orderQuery = new OrderQuery(pageSize,page,number,beginTime,endTime);
        Page<Orders> ordersPage = new Page<>(orderQuery.getPage(),orderQuery.getPageSize());
        Page<OrdersDto> pageInfo = orderService.pageWithOrders(ordersPage,orderQuery);
        log.info("订单分页查询");
        return R.success(pageInfo);
    }

    /**
     * 用户提交订单
     * @author chenxys
     * @param orders 订单信息
     * @return com.gec.common.R<java.lang.String>
     */
    @PostMapping("/submit")
    public R<String> submit(@RequestBody Orders orders){
        log.info("用户下单");
        orderService.submit(orders);
        return R.success("下单成功");
    }

    /**
     * 历史订单信息
     * @author chenxys
     * @param page 当前页
     * @param pageSize 每页显示条数
     * @return com.gec.common.R<com.baomidou.mybatisplus.extension.plugins.pagination.Page>
     */
    @GetMapping("/userPage")
    public R<Page<OrdersDto>> userPage(Long page,Long pageSize){
        Page<OrdersDto> ordersDtoPage = orderService.getUserPage(page,pageSize);
        return R.success(ordersDtoPage);
    }

    /**
     * 更改订单状态
     * @author chenxys
     * @param orders 订单
     * @return com.gec.common.R<java.lang.String>
     */
    @PutMapping
    public R<String> updateStatus(@RequestBody Orders orders){
        orderService.updateStatus(orders);
        return R.success("修改成功");
    }
}
